import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:gameplay_flutter/lib_baseview/lib_baseview.dart';
import 'package:gameplay_flutter/lib_runtime/extension/string_extension/string_text.dart';
import 'package:gameplay_flutter/lib_runtime/extension/widget/widget_wrapper.dart';
import 'package:gameplay_flutter/resource/language/generated/l10n.dart';
import 'package:ones/ui.dart';

import '../../interface/root_proxy_model.dart';

class MenuPlayDialog extends StatefulWidget {
  /// 房间所有者（是否是房主）
  final bool? owner;
  MenuPlayDialog({Key? key, required this.owner});

  @override
  State<MenuPlayDialog> createState() => _MenuPlayDialogState();

  static Future<void> show(BuildContext context, bool? romOwner) async {
    return showModalBottomSheet(
      context: context,
      isScrollControlled: true,
      backgroundColor: Colors.black,
      builder: (context) {
        return MenuPlayDialog(owner: romOwner);
      },
    );
  }
}

class _MenuPlayDialogState extends State<MenuPlayDialog> {
  List<Map> items = [];

  @override
  void initState() {
    super.initState();

    if (widget.owner ?? false) {
      items = [
        {
          'title': T.current.room11_share,
          'image': 'room/room_setting/room_share.png',
          'action': 'share',
        },
        {
          'title': T.current.room11_edit,
          'image': 'room/room_setting/room_edit.png',
          'action': 'edit',
        },
        {
          'title': T.current.room11_member,
          'image': 'room/room_setting/room_member.png',
          'action': 'member',
        },
        {
          'title': T.current.room11_number,
          'image': 'room/room_setting/room_numbers.png',
          'action': 'number',
        },
        {
          'title': T.current.room11_wallpaper,
          'image': 'room/room_setting/room_wall_paper.png',
          'action': 'wallpaper',
        },
        {
          'title': T.current.room11_music,
          'image': 'room/room_setting/room_music.png',
          'action': 'music',
        },
        {
          'title': T.current.room11_privacy,
          'image': 'room/room_setting/room_privacy.png',
          'action': 'privacy',
        },
        {
          'title': T.current.room11_lock,
          'image': 'room/room_setting/room_lock.png',
          'action': 'lock',
        },
        {
          'title': T.current.room11_public_screen,
          'image': 'room/room_setting/room_music.png',
          'action': 'public_screen',
        },
        {
          'title': T.current.room11_mode,
          'image': 'room/room_setting/room_privacy.png',
          'action': 'mode',
        },
        {
          'title': T.current.room11_ear_monitoring,
          'image': 'room/room_setting/room_headphones.png',
          'action': 'ear_monitoring',
        }
      ];
    } else {
      items = [
        {
          'title': T.current.room11_share,
          'image': 'room/room_setting/room_share.png',
          'action': 'share',
        },
        {
          'title': T.current.room11_wallpaper,
          'image': 'room/room_setting/room_wall_paper.png',
          'action': 'wallpaper',
        },
        {
          'title': T.current.room11_music,
          'image': 'room/room_setting/room_music.png',
          'action': 'music',
        },
        {
          'title': T.current.room11_ear_monitoring,
          'image': 'room/room_setting/room_headphones.png',
          'action': 'ear_monitoring',
        },
        {
          'title': T.current.room11_public_screen,
          'image': 'room/room_setting/room_music.png',
          'action': 'public_screen',
        }
      ];
    }
  }

  @override
  Widget build(BuildContext context) {
    final double contentHeight = (widget.owner ?? false) ? 320 : 243;
    return Stack(
      alignment: AlignmentDirectional.bottomEnd,
      children: [
        SizedBox(
          width: ScreenUtil().screenWidth,
          height: contentHeight,
          child: image(
            'room/room_bg_setting.png',
            fit: BoxFit.fitWidth,
          ).corner2(radius: 18),
        ),
        Positioned(
          top: 0,
          left: 0,
          bottom: 0,
          right: 0,
          child: content(contentHeight),
        ),
      ],
    );
  }

  // 定义点击处理函数
  void _onItemClick(String item) {
    if (item == 'share') {
      RootProxyModel.instance?.roomPageModel?.roomToolsShare();
    } else if (item == 'edit') {
      RootProxyModel.instance?.roomPageModel?.roomToolsEdit();
    } else if (item == 'member') {
      RootProxyModel.instance?.roomPageModel?.roomToolsMember();
    } else if (item == 'number') {
      RootProxyModel.instance?.roomPageModel?.roomToolsNumber();
    } else if (item == 'wallpaper') {
      RootProxyModel.instance?.roomPageModel?.roomToolsWallpaper();
    } else if (item == 'music') {
      RootProxyModel.instance?.roomPageModel?.roomToolsMusic();
    } else if (item == 'privacy') {
      RootProxyModel.instance?.roomPageModel?.roomToolsPrivacy();
    } else if (item == 'lock') {
      RootProxyModel.instance?.roomPageModel?.roomToolsLock();
    } else if (item == 'public_screen') {
      RootProxyModel.instance?.roomPageModel?.roomToolsPublicScreen();
    } else if (item == 'mode') {
      RootProxyModel.instance?.roomPageModel?.roomToolsMode();
    } else if (item == 'ear_monitoring') {
      RootProxyModel.instance?.roomPageModel?.roomToolsEarMonitoring();
    } else {
      print('没有对应的action');
    }
  }

  Widget content(double height) {
    return Column(
      // mainAxisAlignment: MainAxisAlignment.start,
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        Padding(
          padding: const EdgeInsetsDirectional.only(start: 15, top: 17),
          child: T.current.room11_room_settings.text(
            color: Colors.white,
            fontSize: 22,
            weight: TextFontWeight.medium,
          ),
        ),
        const SizedBox(height: 5),
        Container(
          height: height - 53,
          width: ScreenUtil().screenWidth,
          child: GridView.builder(
            physics: const NeverScrollableScrollPhysics(), // 设置禁止滚动
            gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(
              crossAxisCount: 4,
              childAspectRatio: 1.2,
              mainAxisSpacing: 1, // 设置主轴方向（垂直方向）的间距为 24
              crossAxisSpacing: 1, // 设置交叉轴方向（水平方向）的间距为 24
            ),
            itemCount: items.length,
            itemBuilder: (context, index) {
              String titleStr = items[index]['title'];
              String imageStr = items[index]['image'];
              String actionStr = items[index]['image'];

              return Column(
                children: [
                  image(imageStr, width: 48, height: 48),
                  titleStr.text(color: const Color(0xFFCACACA), fontSize: 12),
                ],
              ).onClick(() {
                _onItemClick(actionStr);
              });
            },
          ),
        )
      ],
    );
  }
}
